package cn.net.isir.controller.manager;

import cn.net.isir.common.ResultObject;
import cn.net.isir.common.TokenManager;
import cn.net.isir.entity.Shop;
import cn.net.isir.entity.TimeRange;
import cn.net.isir.service.CRMUserManagerService;
import cn.net.isir.service2.TotalFlowShopStatisticsService;
import cn.net.isir.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author liuxubo
 * @filename TotalFlowShopStatisticsController.java
 * @package cn.net.isir.controller.manager
 * @email lxb@isir.net.cn
 * @date 2019/03/11
 * @comment
 * @since 1.8
 */
@RestController
@RequestMapping("/shopstatistics")
public class TotalFlowShopStatisticsController {
    @Resource
    private TotalFlowShopStatisticsService totalFlowShopStatisticsService;
    @Resource
    private CRMUserManagerService crmUserManagerService;

    /**
     * 店铺营业额统计列表
     *
     * @param shop 分页信息
     * @return 列表
     */
    @RequestMapping(value = "/shoplist", method = RequestMethod.GET)
    public ResultObject getTotalShopList(Shop shop) {
        Map<String, List<TimeRange>> salesman = crmUserManagerService.getSalesmanTimeRangeMapByPUserId(TokenManager.getTokenId());
        List<TimeRange> timeRangeList = crmUserManagerService.getTimeRangeList(TokenManager.getTokenId());
        salesman.put(TokenManager.getTokenId(),timeRangeList);
        if (salesman.size() == 0) {
            return new ResultObject(501, "未找到销售人员", new PageInfo<>());
        }
        JSONObject jo = totalFlowShopStatisticsService.getTotalFlowShop(salesman,shop.getName(), crmUserManagerService.getTurnedShopIds(),
                shop.getPageNo(), shop.getPageSize());
        return new ResultObject(200, "success", jo);
    }

    /**
     * 店铺营业额统计列表
     *
     * @param shop 分页信息
     * @return 列表
     */
    @RequestMapping(value = "/chart", method = RequestMethod.GET)
    public ResultObject getTotalShopChart(Shop shop) {
        if (StringUtils.isBlank(shop.getId())) {
            return new ResultObject(500, "id is blank", null);
        }
        Map<String, List<TimeRange>> salesman = crmUserManagerService.getSalesmanTimeRangeMapByPUserId(TokenManager.getTokenId());
        salesman.put(TokenManager.getTokenId(),crmUserManagerService.getTimeRangeList(TokenManager.getTokenId()));
        Map<String, Map<String, BigDecimal>> map = new LinkedHashMap<>();
        map.put("days", totalFlowShopStatisticsService.getDaysFlowAtMonth(salesman, shop.getId()));
        map.put("months", totalFlowShopStatisticsService.getMonthsFlowAtYear(salesman, shop.getId()));
        map.put("quarters", totalFlowShopStatisticsService.getQuarterFlowAtYear(salesman, shop.getId()));
        return new ResultObject(200, "success", map);
    }

}
